package com.dd.controller;

import com.dd.model.Delivery;
import com.dd.service.DeliveryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 配送控制器
 */
@RestController
@RequestMapping("/deliveries")
public class DeliveryController {
    
    @Autowired
    private DeliveryService deliveryService;
    
    /**
     * 获取所有配送信息
     */
    @GetMapping
    public ResponseEntity<List<Delivery>> getAllDeliveries() {
        List<Delivery> deliveries = deliveryService.findAllDeliveries();
        return ResponseEntity.ok(deliveries);
    }
    
    /**
     * 根据ID获取配送信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<?> getDelivery(@PathVariable("id") Integer id) {
        Delivery delivery = deliveryService.getDeliveryById(id);
        if (delivery == null) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "配送信息不存在");
            return ResponseEntity.badRequest().body(response);
        }
        return ResponseEntity.ok(delivery);
    }
    
    /**
     * 根据订单ID获取配送信息
     */
    @GetMapping("/order/{orderId}")
    public ResponseEntity<?> getDeliveryByOrderId(@PathVariable("orderId") Integer orderId) {
        Delivery delivery = deliveryService.getDeliveryByOrderId(orderId);
        if (delivery == null) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "该订单的配送信息不存在");
            return ResponseEntity.badRequest().body(response);
        }
        return ResponseEntity.ok(delivery);
    }
    
    /**
     * 根据配送状态获取配送信息
     */
    @GetMapping("/status/{status}")
    public ResponseEntity<List<Delivery>> getDeliveriesByStatus(@PathVariable("status") String status) {
        List<Delivery> deliveries = deliveryService.findDeliveriesByStatus(status);
        return ResponseEntity.ok(deliveries);
    }
    
    /**
     * 添加配送信息
     */
    @PostMapping
    public ResponseEntity<Map<String, Object>> addDelivery(@RequestBody Delivery delivery) {
        Map<String, Object> response = new HashMap<>();
        
        // 检查是否已存在该订单的配送信息
        Delivery existingDelivery = deliveryService.getDeliveryByOrderId(delivery.getOrderId());
        if (existingDelivery != null) {
            response.put("success", false);
            response.put("message", "该订单已有配送信息");
            return ResponseEntity.badRequest().body(response);
        }
        
        boolean result = deliveryService.addDelivery(delivery);
        
        if (result) {
            response.put("success", true);
            response.put("message", "添加配送信息成功");
            response.put("deliveryId", delivery.getDeliveryId());
        } else {
            response.put("success", false);
            response.put("message", "添加配送信息失败");
        }
        
        return ResponseEntity.ok(response);
    }
    
    /**
     * 更新配送信息
     */
    @PutMapping("/{id}")
    public ResponseEntity<Map<String, Object>> updateDelivery(@PathVariable("id") Integer id, 
                                                             @RequestBody Delivery delivery) {
        Map<String, Object> response = new HashMap<>();
        
        // 设置ID
        delivery.setDeliveryId(id);
        
        boolean result = deliveryService.updateDelivery(delivery);
        
        if (result) {
            response.put("success", true);
            response.put("message", "更新配送信息成功");
        } else {
            response.put("success", false);
            response.put("message", "更新配送信息失败，配送信息可能不存在");
        }
        
        return ResponseEntity.ok(response);
    }
    
    /**
     * 更新配送状态
     */
    @PutMapping("/{id}/status/{status}")
    public ResponseEntity<Map<String, Object>> updateDeliveryStatus(@PathVariable("id") Integer id,
                                                                   @PathVariable("status") String status) {
        Map<String, Object> response = new HashMap<>();
        boolean result = deliveryService.updateDeliveryStatus(id, status);
        
        if (result) {
            response.put("success", true);
            response.put("message", "更新配送状态成功");
        } else {
            response.put("success", false);
            response.put("message", "更新配送状态失败，配送信息可能不存在");
        }
        
        return ResponseEntity.ok(response);
    }
    
    /**
     * 删除配送信息
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> deleteDelivery(@PathVariable("id") Integer id) {
        Map<String, Object> response = new HashMap<>();
        boolean result = deliveryService.deleteDelivery(id);
        
        if (result) {
            response.put("success", true);
            response.put("message", "删除配送信息成功");
        } else {
            response.put("success", false);
            response.put("message", "删除配送信息失败，配送信息可能不存在");
        }
        
        return ResponseEntity.ok(response);
    }
}